package com.steven.im.ui.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.steven.im.R;
import com.steven.im.adapter.MycollectAdapter;
import com.steven.im.base.BaseActivity;
import com.steven.im.customprovider.GridItemDecoration;
import com.steven.im.entity.BaseEntity;
import com.steven.im.entity.MycollectEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.GsonUtils;
import com.steven.im.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import retrofit2.Response;

/**
 * 我的收藏
 */
public class MycollectActivity extends BaseActivity {

    @BindView(R.id.rv_mycollect)
    RecyclerView rvMycollect;
    MycollectAdapter mAdapter;
    List<MycollectEntity> mList = new ArrayList<>();

    @Override
    public void setListen() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                startActivity(new Intent(MycollectActivity.this, GoodsDetailActivity.class).putExtra("goodId", mList.get(position).getGoodsId()).putExtra("issuedNo", mList.get(position).getIssuedNo()));
            }
        });
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("我的收藏")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_mycollect;
    }

    @Override
    public void initView() {
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        rvMycollect.setLayoutManager(layoutManager);
        mAdapter = new MycollectAdapter(this, mList);
        rvMycollect.setAdapter(mAdapter);
        GridItemDecoration divider = new GridItemDecoration.Builder(Objects.requireNonNull(this))
                .setHorizontalSpan(R.dimen.common_vew_column_padding)
                .setVerticalSpan(R.dimen.common_vew_raw_padding)
                .setColorResource(R.color.bg_gray)
                .setShowLastLine(false)
                .build();
        rvMycollect.addItemDecoration(divider);
    }

    @Override
    public void initData() {
        //获取我的收藏
        Task.getApiService().getMyFavList().enqueue(new MyCallback<BaseEntity>(MycollectActivity.this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                try {

                    if ("0".equals(response.body().getCode())) {
                        List<MycollectEntity> favList = GsonUtils.jsonToArrayList(
                                response.body().getResult().getJSONArray("favoriteList").toString(),
                                MycollectEntity.class);
                        if (favList != null) {
                            mList.clear();
                            mList.addAll(favList);
                            mAdapter.setNewData(mList);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });

        /*
        for (int i = 0; i < 21; i++) {
            MycollectEntity col = new MycollectEntity();
            mList.add(col);
        }
        mAdapter.setNewData(mList);
        */
    }

}
